//
//  NSString+M1Thum.m
//  NewMarryU
//
//  Created by apple on 2018/7/17.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "NSString+M1Thum.h"
#import <AVFoundation/AVFoundation.h>
#import <CommonCrypto/CommonDigest.h>
@implementation NSString (M1Thum)
- (UIImage *)m1_ThumbImageWithSandBox:(BOOL)isSandBox
{
    NSURL *url = isSandBox ? [NSURL fileURLWithPath:self] : [NSURL URLWithString:self];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}
-(NSUInteger)m1_cLength{
    NSUInteger m1_strlength = 0;
    char* m1_p = (char*)[self cStringUsingEncoding:NSUTF8StringEncoding];
    for (int i=0 ; i<[self lengthOfBytesUsingEncoding:NSUTF8StringEncoding] ;i++) {
        if (*m1_p) {
            if(*m1_p == '\xe4' || *m1_p == '\xe5' || *m1_p == '\xe6' || *m1_p == '\xe7' || *m1_p == '\xe8' || *m1_p == '\xe9')
            {
                m1_strlength--;
            }
            m1_p++;
            m1_strlength++;
        }
        else {
            m1_p++;
        }
    }
    return m1_strlength;
}
-(CGSize)m1_size_With_Font:(UIFont *)font constrainedToSize:(CGSize)size{
    return [self boundingRectWithSize: size
                              options: NSStringDrawingUsesLineFragmentOrigin
                           attributes: [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]
                              context: nil].size;
}
-(NSString *)m1_removeWhitespace{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
-(BOOL)m1_includEmoji{
    __block BOOL m1_isIncluded = NO;
    NSString *m1_others = @"➋➌➍➎➏➐➑➒";
    if (self.length == 1 &&[m1_others rangeOfString:self].location !=NSNotFound) {
        return NO;
    }
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                             options:NSStringEnumerationByComposedCharacterSequences
                          usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                              const unichar m1_hs = [substring characterAtIndex:0];
                              if (0xd800 <= m1_hs && m1_hs <= 0xdbff) {
                                  if (substring.length > 1) {
                                      const unichar ls = [substring characterAtIndex:1];
                                      const int uc = ((m1_hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                      if (0x1d000 <= uc && uc <= 0x1f77f) {
                                          m1_isIncluded = YES;
                                      }
                                  }
                              } else if (substring.length > 1) {
                                  const unichar ls = [substring characterAtIndex:1];
                                  if (ls == 0x20e3) {
                                      m1_isIncluded = YES;
                                  }
                              } else {
                                  if (0x2100 <= m1_hs && m1_hs <= 0x27ff) {
                                      m1_isIncluded = YES;
                                  } else if (0x2B05 <= m1_hs && m1_hs <= 0x2b07) {
                                      m1_isIncluded = YES;
                                  } else if (0x2934 <= m1_hs && m1_hs <= 0x2935) {
                                      m1_isIncluded = YES;
                                  } else if (0x3297 <= m1_hs && m1_hs <= 0x3299) {
                                      m1_isIncluded = YES;
                                  } else if (m1_hs == 0xa9 || m1_hs == 0xae || m1_hs == 0x303d || m1_hs == 0x3030 || m1_hs == 0x2b55 || m1_hs == 0x2b1c || m1_hs == 0x2b1b || m1_hs == 0x2b50) {
                                      m1_isIncluded = YES;
                                  }else if (m1_hs == 0x200d){
                                      m1_isIncluded = YES;
                                  }
                              }
                          }];
    return m1_isIncluded;
}

-(NSString *)m1_MD5{
    const char *m1_cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( m1_cStr, (unsigned int)strlen(m1_cStr), result );
    return [NSString stringWithFormat:  @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",result[0],result[1],result[2],result[3],result[4],result[5],result[6],result[7],result[8],result[9],result[10],result[11],result[12],result[13],result[14],result[15]];
}

@end
